package com.breakfast.fun.ui.user.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.breakfast.fun.R;
import com.breakfast.fun.adapter.user.GiftAdapter;
import com.breakfast.fun.base.BaseActivity;
import com.breakfast.fun.data.entity.user.coupon.Coupon;
import com.breakfast.fun.ui.user.coupon.CouponActivity;
import com.breakfast.fun.util.DensityUtil;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.ScreenUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

public class GiftActivity extends BaseActivity<GiftPresenter, GiftModel> implements GiftContract.View {
    @Bind(R.id.iv_close_gift)
    ImageView mIvCloseGift;
    @Bind(R.id.fl_gift)
    FrameLayout mFlGift;
    @Bind(R.id.lv_gift)
    ListView mLvGift;
    @Bind(R.id.activity_gift)
    RelativeLayout mActivityGift;
    @Bind(R.id.btn_gift)
    ImageButton mBtnGift;

    private List<Coupon.CouponData> mList = new ArrayList<Coupon.CouponData>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_gift;
    }

    @Override
    public void initView() {

        mIvCloseGift.setOnClickListener(v -> {
            this.finish();
            overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
        });

        mBtnGift.setOnClickListener(v -> {
            Intent intent = new Intent(GiftActivity.this, CouponActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("couponList", (Serializable) mList);
            intent.putExtra("isGiftActivity", true);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtras(bundle);
            GiftActivity.this.startActivity(intent);
            this.finish();
        });
    }

    @Override
    public void showGift(Coupon coupon) {
        mList.addAll(coupon.getData());
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.gift_activity_anim);
        animation.setInterpolator(new BounceInterpolator());
        animation.setDuration(800);
        animation.setFillAfter(true);
        mActivityGift.setAnimation(animation);
        animation.start();
        GiftAdapter adapter = new GiftAdapter(context, coupon.getData());
        mLvGift.setAdapter(adapter);
        View headView = LayoutInflater.from(this).inflate(R.layout.lv_gift_head, null);
        View footView = LayoutInflater.from(this).inflate(R.layout.lv_gift_foot, null);
        mLvGift.addHeaderView(headView);
        mLvGift.addFooterView(footView);
        mActivityGift.setVisibility(View.VISIBLE);
    }

    @Override
    public void closeActivity() {
        this.finish();
    }


    @Override
    public void finish() {

        overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
        super.finish();
    }

}
